unit miscfunc;
(*  cssMonkey
    Copyright (C) 2011 Matthew Hipkin <http://www.matthewhipkin.co.uk>

    This Source Code Form is subject to the terms of the Mozilla Public
    License, v. 2.0. If a copy of the MPL was not distributed with this
    file, You can obtain one at http://mozilla.org/MPL/2.0/. *)
{$mode objfpc}{$H+}

interface

uses
  httpsend, StdCtrls, Menus, Classes, SysUtils, ComCtrls, ExtCtrls, SynEdit
  {$IFDEF MSWINDOWS}, Windows{$ENDIF}, Graphics;

type
  TArray = array of string;

type TTabItem = packed record
  tab: TTabSheet;
  tree: TTreeView;
  edit: TSynEdit;
  s: TSplitter;
  localFilename: String;
  URL: String;
  isSaved: Boolean;
  menu: TMenuItem;
end;

type TFavItem = packed record
  key: String;
  values: TArray;
  button: TButton;
  menu: TMenuItem;
  showButton: Boolean;
end;

const
  APPVER = '0.1a';
  CURRVER = 20130206;
  HELPBASEURL = 'http://www.matthewhipkin.co.uk/res/cssmonkeydoc/';

var
  appdir: String;
  qLinks: TList;

{$IFDEF MSWINDOWS}function getWinVer: String;{$ENDIF}
function explode(cDelimiter,  sValue : string; iCount : integer) : TArray;
function httpGet(URL: String): String;
procedure httpGetStream(URL: String; var s: TMemoryStream);
function ColourToHTML(inCol: TColor): String;
function HexToTColor(sColor: string): TColor;
function stripEverything(inStr: String): String;

implementation

{$IFDEF MSWINDOWS}
function getWinVer: String;
var
  VerInfo: TOSVersioninfo;
begin
  VerInfo.dwOSVersionInfoSize := SizeOf(TOSVersionInfo);
  GetVersionEx(VerInfo);
  Result := 'Windows NT '+IntToStr(VerInfo.dwMajorVersion) + '.' + IntToStr(VerInfo.dwMinorVersion)
end;
{$ENDIF}

{ explode function taken from
  http://www.jasonwhite.co.uk/delphi-explode-function-like-php-explode/ }
function explode(cDelimiter,  sValue : string; iCount : integer) : TArray;
var
  s : string; i,p : integer;
begin
  s := sValue; i := 0;
  while length(s) > 0 do
  begin
    inc(i);
    SetLength(result, i);
    p := pos(cDelimiter,s);
    if ( p > 0 ) and ( ( i < iCount ) OR ( iCount = 0) ) then
    begin
      result[i - 1] := copy(s,0,p-1);
      s := copy(s,p + length(cDelimiter),length(s));
    end else
    begin
      result[i - 1] := s;
      s :=  '';
    end;
  end;
end;

function httpGet(URL: String): String;
var
  HTTP: THTTPSend;
  l: TStrings;
  OS: String;
begin
  Result := '';
  {$ifdef Windows}
    OS := getWinVer;
  {$endif}
  {$ifdef Linux}
    OS := 'Linux';
  {$endif}
  {$ifdef FreeBSD}
    OS := 'FreeBSD';
  {$endif}
  {$ifdef Darwin}
    OS := 'OSX';
  {$endif}
  l := TStringList.Create;
  HTTP := THTTPSend.Create;
  HTTP.UserAgent := 'cssMonkey/'+APPVER+' ('+OS+'; '+IntToStr(CURRVER)+'; +http://www.matthewhipkin.co.uk/apps/cssmonkey/)';
  if HTTP.HTTPMethod('GET', URL) then
  begin
    l.LoadFromStream(Http.Document);
    Result := l.Text;
  end;
  HTTP.Free;
  l.Free;
end;

procedure httpGetStream(URL: String; var s: TMemoryStream);
var
  HTTP: THTTPSend;
  OS: String;
begin
  {$ifdef Windows}
    OS := getWinVer;
  {$endif}
  {$ifdef Linux}
    OS := 'Linux';
  {$endif}
  {$ifdef FreeBSD}
    OS := 'FreeBSD';
  {$endif}
  {$ifdef Darwin}
    OS := 'OSX';
  {$endif}
  HTTP := THTTPSend.Create;
  HTTP.UserAgent := 'cssMonkey/'+APPVER+' ('+OS+'; '+IntToStr(CURRVER)+'; +http://www.matthewhipkin.co.uk/apps/cssmonkey/)';
  if HTTP.HTTPMethod('GET', URL) then
  begin
    s.Clear;
    s.LoadFromStream(Http.Document);
  end;
  HTTP.Free;
end;

function HexToTColor(sColor: string): TColor;
begin
  Result :=
    RGBToColor(
      { get red value }
      StrToInt( '$'+Copy( sColor, 2, 2 ) ),
      { get green value }
      StrToInt( '$'+Copy( sColor, 4, 2 ) ),
      { get blue value }
      StrToInt( '$'+Copy( sColor, 6, 2 ) )
    );
end;

function ColourToHTML(inCol: TColor): String;
var
  i: Integer;
  S: String;
begin
  i := Integer(inCol);
  S := '#';
  S := S + copy(IntToHex(i, 7), 6, 2);
  S := S + copy(IntToHex(i, 7), 4, 2);
  S := S + copy(IntToHex(i, 7), 2, 2);
  Result := S;
end;

function stripEverything(inStr: String): String;
var
  x: integer;
  stripChars: set of char;
  newStr: String;
  add: Boolean;
begin
  stripChars := [' ',#13,#10];
  newStr := '';
  for x := 1 to Length(inStr) do
  begin
    add := true;
    if inStr[x] in stripChars then add := false;
    if add then newStr := newStr + inStr[x];
  end;
  Result := newStr;
end;

end.

